package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.crm.entity.FsDirectory;
import com.ruiysoft.crm.entity.FsDirectoryEx;
import com.ruiysoft.crm.model.entity.SysUser;
import com.ruiysoft.crm.service.IFsDirectoryService;
import com.ruiysoft.crm.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分机号和网关，关联表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-03-04
 */
@RestController
@RequestMapping("/fsdirectory")
public class FsDirectoryController {
    @Autowired
    IFsDirectoryService fsDirectoryService;

    @Autowired
    SysUserService sysUserService;

    @GetMapping("/list")
    public Object list(@RequestHeader String userId) {
        Map<String, Object> map = new HashMap<>();

        SysUser sysUser = sysUserService.getById(userId);
        if(sysUser != null) {
            map.put("rows", fsDirectoryService.listLeftJoinGateWay(sysUser.getEnterpriseId()));
            map.put("totalCount", fsDirectoryService.count(new QueryWrapper<FsDirectory>().eq("enterpriseCode", sysUser.getEnterpriseId())));
        }

        return map;
    }

    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody FsDirectory fsDirectory, @RequestHeader String userId) {
        SysUser sysUser = sysUserService.getById(userId);
        if(sysUser != null) {
            if(fsDirectory != null) {
                fsDirectory.setEnterpriseCode(sysUser.getEnterpriseId());
                boolean rtn = fsDirectoryService.saveOrUpdate(fsDirectory);
                return rtn;
            }
        }

        return -1;
    }

    @DeleteMapping("/removeById")
    public Object removeById(Integer id) {
        return fsDirectoryService.removeById(id);
    }
}
